var playerRegistry = {};
var flowplayer = flowplayer || {};

function createPlayer(media, startTime, callback) {
	if (media == null) return;
	if (media.type == null) {
		media.type = 'Red5'; // the default
	}
	var player = playerRegistry[media.type];
	player.install("fms", media, startTime, callback);
	return player;

}

$(function() {
	var player = {};
    player.install = function(divId, media, startTime, callback) {
		installFlowplayer(divId, media, startTime, callback);
	}
	player.seek = function(time) {
		$f(flowplayer.divId).seek(time);
	}
	player.time = function() {
		var playerStatus = $f(flowplayer.divId).getStatus();
		return playerStatus.time;
	}
	player.play = function() {
		var p = $f(flowplayer.divId);
		if (p.isPlaying()) {
			p.pause();
		} else {
			p.play();
		}
	}
	playerRegistry['Red5'] = player;

})



function installFlowplayer(divId, media, startTime, callback) {
	flowplayer.divId = divId;
	$f(divId, {src: 'scripts/flowplayer/flowplayer-3.1.1.swf', wmode: 'transparent'}, {
			clip: {
					provider: 'influxis',
					onStart: function(clip) { 
							callback();
						},
					streams: [
							{ url: media.media, start: startTime }
					]
			},
			// streaming plugins are configured under the plugins node
			plugins: {
				 controls: {
				   fullscreen: false,
				   backgroundColor: '#FFFFFF',
				   backgroundGradient: 'low',
				   all: false,
				   scrubber: false,
				   mute: false,
				   height: 30,
				   borderRadius: '2px'
				 },
				 time: {
					 url: 'scripts/flowplayer/flowplayer.controls-3.1.1.swf',
					 backgroundColor: '#FFFFFF',
					 backgroundGradient: 'low',
					 top: 0,
					 all: false,
					 play: true,
					 time: true,
					 volume: true,
					 mute: true,
					 height: 30
				 },
				// here is our rtpm plugin configuration
				influxis: {
						url: 'scripts/flowplayer/flowplayer.rtmp-3.1.0.swf',
						// netConnectionUrl defines where the streams are found
						netConnectionUrl: media.url
				}
			}
	});
}



function installFlowplayerForMediaStream(divId, baseUrl, playlist, callback) {
	flowplayer.divId = divId;
	$f(divId, {src: 'scripts/flowplayer/flowplayer-3.1.5.swf', wmode: 'transparent'}, {
			clip: {
				provider: 'influxis',
				onStart: function(clip) { 
						callback(clip);
				},
				autoPlay : false
			},
			playlist : playlist,
			// streaming plugins are configured under the plugins node
			plugins: {
				 controls: {
				   fullscreen: false,
				   backgroundColor: '#FFFFFF',
				   backgroundGradient: 'low',
				   all: false,
				   playlist: true,
				   scrubber: false,
				   mute: false,
				   height: 30,
				   borderRadius: '2px'
				 },
				 time: {
					 url: 'scripts/flowplayer/flowplayer.controls-3.1.5.swf',
					 backgroundColor: '#FFFFFF',
					 backgroundGradient: 'low',
					 top: 0,
					 all: false,
					 play: true,
					 time: true,
					 volume: true,
					 mute: true,
					 height: 30
				 },
				// here is our rtpm plugin configuration
				influxis: {
						url: 'scripts/flowplayer/flowplayer.rtmp-3.1.3.swf',
						// netConnectionUrl defines where the streams are found
						netConnectionUrl: baseUrl
				}
			}
	});
}

